#include "TeacherMyStudentInfo.h"
#include "ui_TeacherMyStudentInfo.h"
#include <QDebug>
#include <QSqlError>
#include <QHeaderView>
#include <QTableWidgetItem>
#include <QLabel>
#include <QPixmap>
#include <QAxObject>
#include <QFileDialog>
#include <QInputDialog>
#include <QMessageBox>
#include <QTimer>
#include <QToolTip>
#include <QMenu>
#include <QAction>
#include <QContextMenuEvent>
#include "Admin/AdminExcel.h"
#include "TeachInterface.h"
#include "Teacher/TeacherStudentScore.h"

TeacherMyStudentInfo::TeacherMyStudentInfo(QWidget *parent) :
    QWidget(parent),  // 注意初始化列表
    ui(new Ui::TeacherMyStudentInfo)  // 初始化ui成员
{
    ui->setupUi(this);  // 设置UI

    // 设置表格表头
    QStringList headers;
    headers << "学号" << "班级" << "姓名" << "性别" << "出生日期" << "手机号" << "学习阶段" << "就业去向" << "任课教师" << "缴费状态";
    ui->tableWidget->setColumnCount(10);
    ui->tableWidget->setHorizontalHeaderLabels(headers);
    ui->tableWidget->setRowCount(0);
    ui->tableWidget->verticalHeader()->setVisible(false);

    // 设置表头自动拉伸
    ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);

    // 设置表格自动排序
    ui->tableWidget->setSortingEnabled(true);

    ui->tableWidget->resizeColumnsToContents();  // 自动调整所有列的宽度以适应内容

    // 设置表格内的字体颜色为白色
    for (int row = 0; row < ui->tableWidget->rowCount(); ++row)
    {
        for (int col = 0; col < ui->tableWidget->columnCount(); ++col)
        {
            QTableWidgetItem* item = ui->tableWidget->item(row, col);

            if (item)
            {
                item->setForeground(QBrush(QColor(255, 255, 255)));
            }
        }
    }

    // 设置label样式
    ui->image_label->setPixmap(QPixmap(":/TeacherImage/stu.png"));
    ui->image_label->setStyleSheet("background: transparent;");
    ui->image_label->setFixedSize(80, 80);  // 设置 QLabel 固定为 100x100 大小
    ui->image_label->setScaledContents(true);
    ui->text_label->setStyleSheet("color:white;");

    // 设置pushbutton样式
    ui->export_pushbutton->setStyleSheet("background: transparent;");
    ui->export_pushbutton->setStyleSheet("color:white;");
    ui->filter_pushbutton->setStyleSheet("background: transparent;");
    ui->filter_pushbutton->setStyleSheet("color:white;");

    // 设置背景图片
    this->setStyleSheet("background-image:url(:/TeacherImage/bk3.jpg)");

    // 加载学生数据
    loadStudentData();

    // 连接筛选按钮
    connect(ui->filter_pushbutton, &QPushButton::clicked, this, &TeacherMyStudentInfo::FilterData);
    // 连接导出数据按钮
    connect(ui->export_pushbutton, &QPushButton::clicked, this, &TeacherMyStudentInfo::ExportData);
    //双击进入评语界面
    connect(ui->tableWidget, &QTableWidget::cellDoubleClicked, this, &TeacherMyStudentInfo::onCellDoubleClicked);

}

TeacherMyStudentInfo::~TeacherMyStudentInfo()
{
    delete ui;  // 释放 ui 指针
}

// 导出数据
void TeacherMyStudentInfo::ExportData()
{
    AdminExcel *Export = new AdminExcel;
    Export->SaveExcel();
}

// 筛选函数
void TeacherMyStudentInfo::FilterData()
{
    // 弹出姓名筛选对话框
    QInputDialog* inputDialog = new QInputDialog(this);
    inputDialog->setWindowTitle("输入姓名筛选");
    inputDialog->setLabelText("请输入姓名进行筛选:");

    // 设置文本框背景和文本颜色
    inputDialog->setStyleSheet(
        "QLineEdit {"
        "    color: white; "
        "    background-color: #333; "
        "    border: 1px solid #555; "
        "    font-size: 14px; "
        "    padding: 10px; "
        "} "
        "QLabel {"
        "    color: white; "
        "    font-size: 14px; "
        "} "
        "QDialog {"
        "    min-width: 400px; "
        "    min-height: 150px; "
        "} "
    );

    inputDialog->exec();

    QString name = inputDialog->textValue();  // 获取用户输入的内容

    delete inputDialog;

    if (!name.isEmpty())
    {
        // 调用函数加载数据
        int rowsLoaded = loadStudentData(name);

        if (rowsLoaded == 0)
        {
            // 如果没有找到数据，弹出提示框
            QMessageBox::warning(this, "未找到数据", "没有找到与 \"" + name + "\" 匹配的学生信息！");
        }
    }
    else
    {
        qDebug() << "对话框已取消";
    }
}

// 加载学生数据
int TeacherMyStudentInfo::loadStudentData(const QString &filterName)
{
    bool isFiltered = !filterName.isEmpty();

    // 假数据
    QVector<QVector<QString>> fakeData =
    {
        {"2021001", "1班", "张三", "男", "2000-01-01", "13800000001", "本科", "IT公司", "李老师", "已缴费"},
        {"2021002", "2班", "李四", "女", "1999-05-15", "13800000002", "本科", "金融公司", "王老师", "未缴费"},
        {"2021003", "1班", "王五", "男", "1998-08-20", "13800000003", "硕士", "外企", "赵老师", "已缴费"},
        {"2021004", "2班", "赵六", "女", "2000-03-12", "13800000004", "博士", "教育公司", "李老师", "未缴费"},
    };

    QVector<QVector<QString>> filteredData;

    // 如果有过滤条件，筛选数据
    if (isFiltered)
    {
        for (const auto& student : fakeData)
        {
            if (student[2].contains(filterName, Qt::CaseInsensitive))  // 姓名过滤
            {
                filteredData.push_back(student);
            }
        }
    }
    else
    {
        filteredData = fakeData;  // 没有筛选条件，加载所有数据
    }

    // 清空当前表格内容
    ui->tableWidget->setRowCount(0);

    int currentRow = 0;
    for (const auto& student : filteredData)
    {
        // 插入一行
        ui->tableWidget->insertRow(currentRow);

        // 创建 QTableWidgetItem 并设置内容
        QTableWidgetItem *StudentIDItem = new QTableWidgetItem(student[0]);
        QTableWidgetItem *ClassItem = new QTableWidgetItem(student[1]);
        QTableWidgetItem *NameItem = new QTableWidgetItem(student[2]);
        QTableWidgetItem *SexItem = new QTableWidgetItem(student[3]);
        QTableWidgetItem *DateItem = new QTableWidgetItem(student[4]);
        QTableWidgetItem *PhoneNumberItem = new QTableWidgetItem(student[5]);
        QTableWidgetItem *StageItem = new QTableWidgetItem(student[6]);
        QTableWidgetItem *CompanyNameItem = new QTableWidgetItem(student[7]);
        QTableWidgetItem *CourseTeacherItem = new QTableWidgetItem(student[8]);
        QTableWidgetItem *PaymentStatusItem = new QTableWidgetItem(student[9]);

        // 设置每个单元格的对齐方式
        StudentIDItem->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
        NameItem->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
        SexItem->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
        DateItem->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
        PhoneNumberItem->setTextAlignment(Qt::AlignCenter | Qt::AlignVCenter);
        ClassItem->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
        StageItem->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
        CompanyNameItem->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
        CourseTeacherItem->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
        PaymentStatusItem->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);

        // 设置字体颜色
        StudentIDItem->setForeground(QBrush(QColor(255, 255, 255)));
        ClassItem->setForeground(QBrush(QColor(255, 255, 255)));
        NameItem->setForeground(QBrush(QColor(255, 255, 255)));
        SexItem->setForeground(QBrush(QColor(255, 255, 255)));
        DateItem->setForeground(QBrush(QColor(255, 255, 255)));
        PhoneNumberItem->setForeground(QBrush(QColor(255, 255, 255)));
        StageItem->setForeground(QBrush(QColor(255, 255, 255)));
        CompanyNameItem->setForeground(QBrush(QColor(255, 255, 255)));
        CourseTeacherItem->setForeground(QBrush(QColor(255, 255, 255)));
        PaymentStatusItem->setForeground(QBrush(QColor(255, 255, 255)));

        // 将数据插入表格
        ui->tableWidget->setItem(currentRow, 0, StudentIDItem);
        ui->tableWidget->setItem(currentRow, 1, ClassItem);
        ui->tableWidget->setItem(currentRow, 2, NameItem);
        ui->tableWidget->setItem(currentRow, 3, SexItem);
        ui->tableWidget->setItem(currentRow, 4, DateItem);
        ui->tableWidget->setItem(currentRow, 5, PhoneNumberItem);
        ui->tableWidget->setItem(currentRow, 6, StageItem);
        ui->tableWidget->setItem(currentRow, 7, CompanyNameItem);
        ui->tableWidget->setItem(currentRow, 8, CourseTeacherItem);
        ui->tableWidget->setItem(currentRow, 9, PaymentStatusItem);

        currentRow++;
    }

    // 调整列宽自适应内容
    ui->tableWidget->resizeColumnsToContents();
    return currentRow;
}


void TeacherMyStudentInfo::onCellDoubleClicked(int row, int column)
{
    if (column == 0)  // 假设学号列的索引是0
    {
        // 如果 info_view 已经存在，直接显示并返回
        if (info_view != nullptr)
        {
            info_view->show();
            return;
        }

        // 获取学生学号
        QString studentid = ui->tableWidget->item(row, 0)->text();

        // 初始化并显示 TeachInterface
        info_view = new TeachInterface;
        info_view->InitID(studentid);  // 传递学生学号
        info_view->show();  // 显示新窗口
        this->close();  // 关闭当前窗口
    }
}

void TeacherMyStudentInfo::contextMenuEvent(QContextMenuEvent *event)
{
    // 创建右键菜单
    QMenu contextMenu(this);

    // 创建查看成绩菜单项
    QAction *viewScoreAction = new QAction("查看成绩", this);
    connect(viewScoreAction, &QAction::triggered, this, &TeacherMyStudentInfo::onViewStudentScore);

    // 将菜单项添加到菜单中
    contextMenu.addAction(viewScoreAction);

    // 显示菜单
    contextMenu.exec(event->globalPos());
}

// 右键菜单对应的槽函数
void TeacherMyStudentInfo::onViewStudentScore()
{
    // 获取选中的行
    int row = ui->tableWidget->currentRow();
    if (row == -1) {
        QMessageBox::warning(this, "提示", "请选中一个学生");
        return;
    }

    // 获取学生姓名（假设姓名在第三列，即索引为2）
    QString studentName = ui->tableWidget->item(row, 2)->text();

    // 创建并显示成绩界面
    TeacherStudentScore *scoreView = new TeacherStudentScore;
    scoreView->show();  // 显示学生成绩界面
    scoreView->setStudentName(studentName);  // 传递学生姓名
}
